package org.wlldTest.resnet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.dromara.easyai.config.ResnetConfig;
import org.dromara.easyai.entity.ThreeChannelMatrix;
import org.dromara.easyai.function.ReLu;
import org.dromara.easyai.resnet.ResnetInput;
import org.dromara.easyai.resnet.ResnetManager;
import org.dromara.easyai.resnet.entity.ResnetModel;
import org.dromara.easyai.tools.Picture;
import org.dromara.easyai.yolo.YoloTypeBack;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * @author lidapeng
 * @time 2025/4/17 13:31
 */
public class Test {
    public static ResnetManager resnetManager;
    public static ResnetConfig resnetConfig = new ResnetConfig();
    public static int imageSize = 100;

    public static void main(String[] args) throws Exception {
        init();
        study();
        //test();
    }

    public static void init() throws Exception {
        resnetConfig.setChannelNo(2);
        resnetConfig.setHiddenNerveNumber(16);
        resnetConfig.setSize(imageSize);
        resnetConfig.setShowLog(true);
        resnetConfig.setTypeNumber(3);
        resnetConfig.setStudyRate(0.0001f);//0.001
        resnetConfig.setGaMa(0.92f);
        resnetConfig.setAuto(true);
        resnetConfig.setMinFeatureSize(2);
        resnetConfig.setGMaxTh(100000000f);
        resnetConfig.setRegular(0.00002f);
        resnetConfig.setRegularModel(1);
        resnetManager = new ResnetManager(resnetConfig, new ReLu());
        resnetManager.insertModel(readModel());
    }

    public static void test() throws Exception {
        YoloTypeBack yoloTypeBack = new YoloTypeBack();
        ResBack resBack = new ResBack();
        ResnetInput resnetInput = resnetManager.getRestNetInput();
        ThreeChannelMatrix t1 = Picture.getThreeMatrix("/Users/lidapeng/job/testPic/resnetTest/t1/m21.jpg", false);
        ThreeChannelMatrix t2 = Picture.getThreeMatrix("/Users/lidapeng/job/testPic/resnetTest/t2/m21.jpg", false);
        resnetInput.postThreeChannelMatrix(t1, false, null, yoloTypeBack, 1, false);
        //System.out.println(resBack.getMatrix());
        System.out.println(yoloTypeBack.getId() + ":" + yoloTypeBack.getOut());
        yoloTypeBack.clear();
        resnetInput.postThreeChannelMatrix(t2, false, null, yoloTypeBack, 2, false);
        //System.out.println(resBack.getMatrix());
        System.out.println(yoloTypeBack.getId() + ":" + yoloTypeBack.getOut());
    }

    public static void study() throws Exception {
        Map<Integer, Float> E1 = new HashMap<>();
        Map<Integer, Float> E2 = new HashMap<>();
        Map<Integer, Float> E3 = new HashMap<>();
        E1.put(1, 1f);
        E2.put(2, 1f);
        E3.put(3, 1f);
        ResnetInput resnetInput = resnetManager.getRestNetInput();//8942
        for (int t = 0; t < 8942; t++) {
            String a = "/Users/lidapeng/job/testPic/resnetTest/t1/m" + t + ".jpg";
            String b = "/Users/lidapeng/job/testPic/resnetTest/t2/m" + t + ".jpg";
            String c = "/Users/lidapeng/job/testPic/resnetTest/t3/m" + t + ".jpg";
            ThreeChannelMatrix p1 = Picture.getThreeMatrix(a, false);
            ThreeChannelMatrix p2 = Picture.getThreeMatrix(b, false);
            ThreeChannelMatrix p3 = Picture.getThreeMatrix(c, false);
            System.out.println("t=========" + t);
            resnetInput.postThreeChannelMatrix(p1, true, E1, null, 1, false);
            resnetInput.postThreeChannelMatrix(p2, true, E2, null, 2, false);
            resnetInput.postThreeChannelMatrix(p3, true, E3, null, 3, false);

        }
        ResnetModel resnetModel = resnetManager.getModel();
        writeModel(JSON.toJSONString(resnetModel), "/Users/lidapeng/job/linshi/t1.json");
    }

    private static ResnetModel readModel() {
        File file = new File("/Users/lidapeng/job/linshi/t1.json");
        String a = readPaper(file);
        return JSONObject.parseObject(a, ResnetModel.class);
    }

    private static String readPaper(File file) {
        InputStream read = null;
        String context = null;
        try {
            read = Files.newInputStream(file.toPath());
            byte[] bt = new byte[read.available()];
            read.read(bt);
            context = new String(bt, StandardCharsets.UTF_8);
            read.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (read != null) {
                try {
                    read.close(); //确保关闭
                } catch (IOException el) {
                }
            }
        }
        return context;
    }

    private static void writeModel(String model, String url) throws IOException {//写出模型与 激活参数
        OutputStreamWriter out = new OutputStreamWriter(Files.newOutputStream(Paths.get(url)), StandardCharsets.UTF_8);
        out.write(model);
        out.close();
    }
}
